Public Class NegMail
#Region "PROPIEDADES"
    Private propServidor As String
    Private propCorreoDesde As String
    Private propNombreDesde As String
    Private propCorreoDestino As String
    Private propAsunto As String
    Private propMensaje As String
    Private propEsHTML As Boolean
    Private propMensajeError As String
    Private propListaAttach As ArrayList
    Private propContadorAttach As Integer
#End Region

#Region "ACCESO A PROPIEDADES"
    ''' <summary>
    ''' Servidor De Envio de Correos
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ServidorMail() As String
        Get
            Return Me.propServidor
        End Get
        Set(ByVal value As String)
            Me.propServidor = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Correo desde el que se envia, para que le aparezca al destinatario
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property CorreoDesde() As String
        Get
            Return Me.propCorreoDesde
        End Get
        Set(ByVal value As String)
            Me.propCorreoDesde = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Nombre de quien lo envia
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property NombreDesde() As String
        Get
            Return Me.propNombreDesde
        End Get
        Set(ByVal value As String)
            Me.propNombreDesde = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Correo o correos de destino del mail
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property CorreoDestino() As String
        Get
            Return Me.propCorreoDestino
        End Get
        Set(ByVal value As String)
            Me.propCorreoDestino = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Asunto del correo
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Asunto() As String
        Get
            Return Me.propAsunto
        End Get
        Set(ByVal value As String)
            Me.propAsunto = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Cuerpo principal del Correo
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property CuerpoMensaje() As String
        Get
            Return Me.propMensaje
        End Get
        Set(ByVal value As String)
            Me.propMensaje = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' Establece si el cuerpo del mensaje tiene formato HTML
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property EsHTML() As Boolean
        Get
            Return Me.propEsHTML
        End Get
        Set(ByVal value As Boolean)
            Me.propEsHTML = value
        End Set
    End Property

    ''' <summary>
    ''' Devuelve el mensaje de error producido en el envio
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MensajeError() As String
        Get
            Return Me.propMensajeError
        End Get
    End Property

    ''' <summary>
    ''' Agrega un archivo a la lista de archivos atachados
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public WriteOnly Property AgregarAttach() As String
        Set(ByVal value As String)
            Try
                If System.IO.File.Exists(value.Trim) Then
                    Me.propListaAttach.Add(value)
                    Me.propContadorAttach = Me.propContadorAttach + 1
                End If
            Catch ex As Exception

            End Try
        End Set
    End Property
#End Region

#Region "CONSTRUCTOR"
    Public Sub New()
        Me.propAsunto = ""
        Me.propCorreoDesde = ""
        Me.propCorreoDestino = ""
        Me.propEsHTML = False
        Me.propMensaje = ""
        Me.propMensajeError = ""
        Me.propNombreDesde = ""
        Me.propServidor = ""
        Me.propListaAttach = New ArrayList()
        Me.propContadorAttach = 0
    End Sub
#End Region

#Region "EVENTOS Y FUNCIONES DE LA CLASE"
    ''' <summary>
    ''' Envia el mail con los datos configurados
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function EnvioMail() As Boolean
        Dim bolResultado As Boolean = False
        Try
            Me.propMensajeError = ""
            Dim MyMail As New System.Net.Mail.MailMessage
            Dim SmtpMail As New System.Net.Mail.SmtpClient
            SmtpMail.Credentials = New System.Net.NetworkCredential("informaciones@culturasantander.cl", "CLAVEcorreo")

            For x As Integer = 0 To Me.propCorreoDestino.ToString.Split(";").Length - 1
                MyMail.To.Add((Me.propCorreoDestino.ToString.Split(";")(x)).ToString)
            Next
            MyMail.From = New System.Net.Mail.MailAddress(Me.propCorreoDesde, Me.propNombreDesde, System.Text.Encoding.UTF8)
            MyMail.Subject = Me.propAsunto
            MyMail.Body = Me.propMensaje
            MyMail.IsBodyHtml = Me.propEsHTML

            'REVISAMOS SI EXISTEN ATTACHMENT Y LOS AGREGAMOS
            If Me.propContadorAttach > 0 Then
                For x As Integer = 0 To Me.propContadorAttach
                    MyMail.Attachments.Add(New System.Net.Mail.Attachment(Me.propListaAttach.Item(x)))
                Next
            End If

            SmtpMail.Host = Me.propServidor
            SmtpMail.Send(MyMail)
            MyMail = Nothing

            bolResultado = True
        Catch ex As Exception
            Me.propMensajeError = "Error al GenerarMail - " & ex.ToString
        End Try

        Return bolResultado
    End Function

    ''' <summary>
    ''' Envia el mail con los datos configurados agregados en la copiaOculta
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function EnvioMailCopiaOculta() As Boolean
        Dim bolResultado As Boolean = False
        Try
            Me.propMensajeError = ""
            Dim MyMail As New System.Net.Mail.MailMessage
            Dim SmtpMail As New System.Net.Mail.SmtpClient
            SmtpMail.Credentials = New System.Net.NetworkCredential("informaciones@culturasantander.cl", "CLAVEcorreo")

            For x As Integer = 0 To Me.propCorreoDestino.ToString.Split(";").Length - 1
                If Me.propCorreoDestino.ToString.Split(";")(x).ToString.Trim <> "" Then
                    MyMail.Bcc.Add((Me.propCorreoDestino.ToString.Split(";")(x)).ToString.Trim)
                End If
            Next
            MyMail.From = New System.Net.Mail.MailAddress(Me.propCorreoDesde, Me.propNombreDesde, System.Text.Encoding.UTF8)
            MyMail.Subject = Me.propAsunto
            MyMail.Body = Me.propMensaje
            MyMail.IsBodyHtml = Me.propEsHTML

            'REVISAMOS SI EXISTEN ATTACHMENT Y LOS AGREGAMOS
            If Me.propContadorAttach > 0 Then
                For x As Integer = 0 To Me.propContadorAttach - 1
                    MyMail.Attachments.Add(New System.Net.Mail.Attachment(Me.propListaAttach.Item(x)))
                Next
            End If

            SmtpMail.Host = Me.propServidor
            SmtpMail.Send(MyMail)
            MyMail = Nothing

            bolResultado = True
        Catch ex As Exception
            Me.propMensajeError = "Error al GenerarMail - " & ex.ToString
        End Try

        Return bolResultado
    End Function

    ''' <summary>
    ''' Elimina el Objeto desde Memoria
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Dispose()
        Try
            GC.SuppressFinalize(Me)
        Catch ex As Exception
        End Try
    End Sub
#End Region
End Class
